home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / dict / _ab_tree.c next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  33.0 KB  |  1,272 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _ab_tree.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/impl/ab_tree.h>
  16.  
  17. //---------------------------------------------------------------
  18. //  
  19. //  Michael Wenzel:
  20. //  function insert_at_item added
  21. //
  22. //  double links between leaf and inner node with same key
  23. //
  24. //  delete overwrites copies of keys in inner nodes
  25. //
  26. //  leafs are nodes of degree 1, linked by son[1], son[2]
  27. //
  28. //---------------------------------------------------------------
  29.  
  30. //------------------------------------------------------------------
  31. // constructors
  32. //------------------------------------------------------------------
  33.  
  34.  
  35. ab_tree_node::ab_tree_node(int p_,int height_,int index_,ab_tree_node* father_,int b)
  36. {
  37.     son  = (ab_tree_node**) allocate_words(b+2);   /* array[1..b+1] */
  38.     k    = (GenPtr*) allocate_words(b+1);             /* array[1..b]   */
  39.     same = (ab_tree_node**) allocate_words(b+1);   /* array[1..b]   */
  40.  
  41.  
  42.     // position 0 contains the size of the array:
  43.  
  44.     son[0]  = (ab_tree_node*)(b+2);
  45.     k[0]    = GenPtr(b+1);
  46.     same[0] = (ab_tree_node*)(b+1);
  47.  
  48.     p=p_;
  49.     height=height_;
  50.     index=index_;
  51.     father=father_;
  52.     for(int i=1;i<=(b+1);son[i++]=0);
  53.     for(i=1;i<=b;k[i++]=0);
  54.     for(i=1;i<=b;same[i++]=0);                
  55.    }
  56.  
  57.  
  58.  ab_tree_node::~ab_tree_node() 
  59.   { deallocate_words(son,int(son[0]));    /* first array element gives size */
  60.     deallocate_words(same,int(same[0]));
  61.     deallocate_words(k,int(k[0]));
  62.    }
  63.  
  64.  
  65.  
  66.  
  67.  ab_tree::ab_tree(int a_,int b_)
  68.  {
  69.    root=maximum=minimum=0;
  70.    count=0;
  71.    height=-1;
  72.    if((a_>=2)&&(b_>=2*a_-1))
  73.     { a=a_;
  74.       b=b_;
  75.      }
  76.    else error_handler(1,"ab_tree: illegal arguments (a,b) in tree constructor");
  77.  }
  78.  
  79.  ab_tree::ab_tree(const ab_tree& T)
  80.   {
  81.      if (T.root==0) {root=maximum=minimum=0;height=-1;count=0;}
  82.      else { abnode help=0;
  83.         root = T.copy_ab_tree(T.root,help,T.b);
  84.             maximum=help;
  85.             help=root;
  86.             while (help->p) help=help->son[1];
  87.         minimum=help;
  88.             height=T.height;
  89.             count=T.count;
  90.           };
  91.      a=T.a;
  92.      b=T.b;
  93.   }
  94.  
  95. //-----------------------------------------------------------------
  96. // operators
  97. //-----------------------------------------------------------------
  98.  
  99.  ab_tree& ab_tree::operator=(const ab_tree& T)
  100.   {  if (this == &T) return *this;
  101.  
  102.      clear();
  103.  
  104.      if (T.root!=0) 
  105.      { abnode help=0;
  106.        root=copy_ab_tree(T.root,help,T.b);
  107.        maximum=help;
  108.        help=root;
  109.        while (help->p) help=help->son[1];
  110.        minimum=help;
  111.        height=T.height;
  112.        count=T.count;
  113.       };
  114.  
  115.      a=T.a;
  116.      b=T.b;
  117.  
  118.      return *this;
  119.   }
  120.  
  121.  
  122. //-----------------------------------------------------------------
  123. // member functions
  124. //-----------------------------------------------------------------
  125.  
  126. void ab_tree::exchange_leaves(ab_tree_node* v, ab_tree_node* w)
  127. { // exchange leaves v and w
  128.  
  129.   GenPtr k1 = v->k[1]; 
  130.   int p1 = v->index;
  131.   ab_tree_node* u1 = v->same[1];
  132.   ab_tree_node* f1 = v->father;
  133.  
  134.   GenPtr k2 = w->k[1]; 
  135.   int p2 = w->index;
  136.   ab_tree_node* u2 = w->same[1];
  137.   ab_tree_node* f2 = w->father;
  138.  
  139.  
  140.   f1->son[p1] = w;
  141.   w->index    = p1;
  142.   w->same[1]  = u1;
  143.   w->father   = f1;
  144.  
  145.   f2->son[p2] = v;
  146.   v->index    = p2;
  147.   v->same[1]  = u2;
  148.   v->father   = f2;
  149.  
  150.   ab_tree_node* pred1 = v->son[1];
  151.   ab_tree_node* succ1 = v->son[2];
  152.  
  153.   ab_tree_node* pred2 = w->son[1];
  154.   ab_tree_node* succ2 = w->son[2];
  155.  
  156.   w->son[1] = pred1;
  157.   if (pred1) pred1->son[2] = w;
  158.   if (succ1!=w) 
  159.   { w->son[2] = succ1;
  160.     succ1->son[1] = w;
  161.    }
  162.  
  163.   if (pred2==v) pred2 = w;
  164.  
  165.  
  166.   v->son[1] = pred2;
  167.   v->son[2] = succ2;
  168.   pred2->son[2] = v;
  169.   if (succ2) succ2->son[1] = v;
  170.  
  171.   // minimum & maximum:
  172.   if (v==minimum) minimum = w;
  173.   if (w==maximum) maximum = v;
  174.  
  175.   // overwrite inner copies:
  176.   
  177.   int pos1=1;
  178.   while(u1->same[pos1]!=v) pos1++;
  179.  
  180.   int pos2=1;
  181.   if (u2) while(u2->same[pos2]!=w) pos2++;
  182.  
  183.   u1->k[pos1] = k2;
  184.   u1->same[pos1] = w;
  185.  
  186.   if (u2) 
  187.   { u2->k[pos2] = k1;
  188.     u2->same[pos2] = v;
  189.    }
  190.  
  191. }
  192.  
  193. void ab_tree::reverse_items(ab_tree_node* v, ab_tree_node* w)
  194. { // reverse sequence of leaves: v, ..., w
  195.  
  196.   if (v==w) return;
  197.  
  198. /*
  199.   if (cmp(v->k[1],w->k[1])<0) 
  200.   { newline;
  201.     cout << "a = "; print_key(v->k[1]); newline;
  202.     cout << "b = "; print_key(w->k[1]); newline;
  203.     error_handler(1,"ab_tree: illegal reverse_items(a,b)\n");
  204.    }
  205. */
  206.  
  207.   ab_tree_node* u;
  208.  
  209.   for(;;)
  210.   { exchange_leaves(v,w);
  211.     u = pred(v);
  212.     if (u == w) break;
  213.     v = succ(w);
  214.     if (v==u) break;
  215.     w = u;
  216.    }
  217.  
  218. }
  219.  
  220.  
  221.  void ab_tree::clear()
  222.  { if (root!=0) del_tree(root);
  223.    maximum=minimum=root=0;
  224.    count = 0;
  225.    height=-1;
  226.   }
  227.  
  228.  
  229.  void ab_tree::del(GenPtr key)
  230.  { if (!root) return;                      // s.n.
  231.  
  232.    ab_tree_node* w=locate(key);
  233.  
  234.    if(w && cmp(w->k[1],key) == 0)  del_item(w);
  235.  
  236.    else /* error_handler(1,"ab_tree: del(key):key is not in tree") */;
  237.  }
  238.  
  239.  
  240. ab_tree_node* ab_tree::expand(ab_tree_node* v,int i)
  241.  
  242. // expand v by returning an additional son to the right of w
  243. // --> w is the i-th son of v
  244. // s.n.: if i==0 then  new leftmost son
  245. // expand does not test a<=number of sons(v)<=b
  246. // m.w. expand does not set same links for new leaves
  247.  
  248.  {
  249.    // move the nodes right to w to give an additional son to 
  250.    // the right of w
  251.  
  252.    int l=(v->p);
  253.  
  254.    if (i < l)
  255.    {
  256.      v->son[l+1]=v->son[l];
  257.      v->son[l+1]->index=l+1;
  258.      l--;
  259.     }
  260.  
  261.    while(i < l) 
  262.    { 
  263.      v->k[l+1] = v->k[l];
  264.      v->same[l+1] = v->same[l];
  265.  
  266.      v->son[l+1]=v->son[l];
  267.      v->son[l+1]->index=l+1;
  268.  
  269.      l--;
  270.     };
  271.  
  272.    if (v->height>1)
  273.       v->son[i+1] = new ab_tree_node(0,v->height-1,i+1,v,b);
  274.    else  
  275.       v->son[i+1] = new ab_tree_node(0,v->height-1,i+1,v,1);
  276.  
  277.    (v->p)++;
  278.  
  279.    return v->son[i+1];
  280. }                     
  281.                      
  282.  
  283. void ab_tree::split_node(ab_tree_node* v)
  284. {
  285.  /* adding a child increases the degree of v by 1. If v->p<=b after
  286.     adding the new leave, then we are done . Otherwise we have to 
  287.     split v. Splitting can propagate ==> Loop 
  288.  
  289.     m.w. changes same links between nodes                          */
  290.  
  291.   ab_tree_node* y;
  292.  
  293.   while (v->p==b+1) 
  294.   {
  295.     if (v!=root)  y = v->father;
  296.     else 
  297.     { y=new ab_tree_node(1,v->height+1,0,0,b);
  298.       root=y;height++;
  299.       y->son[1]=v;
  300.       v->index=1;
  301.       v->father=y;
  302.      };
  303.  
  304.  
  305.     register ab_tree_node* u;
  306.  
  307.     u = expand(y,v->index);   // u <-- new right brother of v
  308.  
  309.     int down =(int)((b+1)/2) ;
  310.     int up = (b+1)- down;
  311.  
  312.  
  313.     if (u->index <= b)
  314.     { y->k[u->index] = y->k[v->index];
  315.       y->same[u->index] = y->same[v->index];
  316.      }
  317.  
  318.     y->k[v->index] = v->k[down];
  319.     y->same[v->index]  = v->same[down];
  320.     y->same[v->index]->same[1] = y;    
  321.  
  322.  
  323.     // split v, i.e. take the rightmost (b+1)/2 children and keys
  324.     // away from v and incorporate them into u and store key v->k[(b+1)/2]
  325.     // in y ( = father(v))  between the pointers to v and u i.e. at position
  326.     // v->index
  327.  
  328.     // m.w. change same links of v to y and u
  329.  
  330.     register int j;
  331.  
  332.     for (j=1;j<up;j++)    
  333.     {
  334.       u->son[j] = v->son[down+j];
  335.       u->son[j]->index=j;
  336.       u->son[j]->father=u;
  337.       u->k[j] = v->k[down+j];
  338.       u->same[j] = v->same[down+j]; 
  339.       u->same[j]->same[1] = u;      
  340.  
  341.       v->son[down+j] = 0;      // muss das sein ?
  342.       v->same[down+j] = 0;          
  343.       v->k[down+j] = 0;
  344.  
  345.      };
  346.  
  347.     u->son[up]=v->son[b+1];
  348.     u->son[up]->index=up;
  349.     u->son[up]->father=u;
  350.  
  351.     v->son[b+1] = 0;          // und das?
  352.     v->same[down] = 0;                 
  353.     v->k[down] = 0;
  354.  
  355.     v->p = down;             // change number of children
  356.     u->p = up;
  357.  
  358.     v = y;
  359.   }
  360.  
  361. };
  362.  
  363. ab_tree_node* ab_tree::insert(GenPtr key, GenPtr inf)
  364. {
  365.  
  366.  if (root==0) { root=new ab_tree_node(0,0,0,0,1);
  367.                 copy_key(key);
  368.                 copy_inf(inf);
  369.                 root->inf = inf;
  370.                 root->k[1]= key;
  371.                 height=0;
  372.                 maximum=minimum=root;
  373.                 count++;
  374.                 return root;
  375.                }
  376.  
  377.  // insert_at_item calls copy_key & copy_inf
  378.  
  379.  ab_tree_node* p = locate(key);
  380.  if (p==nil) p = maximum;
  381.  return insert_at_item(p,key,inf);
  382.  
  383. };
  384.  
  385. ab_tree_node* ab_tree::insert_at_item(ab_tree_node* w, GenPtr key, GenPtr inf)
  386.   // insert a new item (key,inf) left or right of leaf w  (according to key(w))
  387.  
  388.    copy_inf(inf);
  389.  
  390.    ab_tree_node* v;
  391.  
  392.     if (cmp(w->k[1],key)!=0)
  393.     {
  394.       copy_key(key);
  395.  
  396.      if ( w!=minimum && cmp(w->son[1]->k[1],key) > 0)
  397.       { cout << "INSERT_AT: WRONG POSITION\n";
  398.         cout << "insert:   key = "; print_key(key); cout << "\n";
  399.         if (w!=maximum) 
  400.            cout << "succ-pos: key = "; print_key(w->son[2]->k[1]); cout << "\n";
  401.         cout << "position: key = "; print_key(w->k[1]); cout << "\n";
  402.         cout << "pred-pos: key = "; print_key(w->son[1]->k[1]); cout << "\n";
  403.         error_handler(1,"ab_tree::insert_at : wrong position "); 
  404.        }
  405.  
  406.      if ( w!=maximum && cmp(w->son[2]->k[1],key) < 0)
  407.       { cout << "INSERT_AT: WRONG POSITION\n";
  408.         cout << "insert:   key = "; print_key(key); cout << "\n";
  409.         cout << "succ-pos: key = "; print_key(w->son[2]->k[1]); cout << "\n";
  410.         cout << "position: key = "; print_key(w->k[1]); cout << "\n";
  411.         if (w!=minimum)
  412.            cout << "pred-pos: key = "; print_key(w->son[1]->k[1]); cout << "\n";
  413.         error_handler(1,"ab_tree::insert_at : wrong position "); 
  414.        }
  415.  
  416.  
  417.         count++;
  418.         if (root==w) {   v=new ab_tree_node(2,1,0,0,b);
  419.                          root=v;height=1;
  420.                          ab_tree_node* u;
  421.                          if (cmp(key,w->k[1])<0)
  422.                              {   
  423.                                  u=new ab_tree_node(0,0,1,v,1);
  424.                                  v->son[1]=u;u->k[1]=key; u->inf=inf;
  425.                                  v->son[2]=w ;
  426.                                  v->p=2;v->k[1]=u->k[1];
  427.                  u->same[1] = v;
  428.                                  v->same[1] = u;
  429.                  w->index=2;
  430.                                  minimum=u;
  431.                                  u->son[2] = w;
  432.                                  w->son[1] = u;
  433.                              }
  434.                          else {
  435.                                  u=new ab_tree_node(0,0,2,v,1);
  436.                                  v->son[1]=w;
  437.                  w->same[1] = v;
  438.                                  v->same[1] = w;
  439.                                  v->son[2]=u;u->k[1]=key; u->inf=inf;
  440.                                  v->p=2;v->k[1]=w->k[1];
  441.                                  w->index=1; 
  442.                                  maximum=u;
  443.                                  w->son[2] = u;
  444.                                  u->son[1] = w;
  445.                               }
  446.                          w->father=v;
  447.  
  448.                          return u;
  449.                       }; 
  450.  
  451.     if ((w!=maximum) && (cmp(key,w->k[1])>0)) w=w->son[2];
  452.  
  453.         ab_tree_node* u;
  454.  
  455.         int index1 = w->index;
  456.  
  457.         v = w->father;
  458.  
  459.         if (cmp(key,w->k[1])<0)
  460.         { u = expand(v,index1-1);   // new son u left of w
  461. /*
  462.                  v
  463.  
  464.               /  |  \
  465.  
  466.                 (u)  w  
  467. */
  468.           u->k[1] = key;
  469.           u->inf = inf;
  470.  
  471.           u->son[2] = w;
  472.           u->son[1] = w->son[1];
  473.           w->son[1] = u;
  474.  
  475.       u->same[1] = v;        
  476.           v->k[index1] = key;
  477.           v->same[index1] = u ;  
  478.  
  479.           if (minimum == w)  
  480.              minimum=u;
  481.           else 
  482.              u->son[1]->son[2] = u;
  483.  
  484.          }
  485.         else 
  486.         { u = expand(v,index1);   // new son u right of w
  487. /*
  488.                  v
  489.  
  490.               /  |  \
  491.  
  492.              w  (u)     
  493. */
  494.           u->k[1] = key;
  495.           u->inf = inf;
  496.  
  497.           u->son[1] = w;
  498.           u->son[2] = w->son[2];
  499.           w->son[2] = u;
  500.  
  501.           if (maximum == w)  
  502.           {
  503.             maximum=u;
  504.             v->k[index1] = w->k[1];
  505.         w->same[1] = v;        
  506.         v->same[index1] = w ;  
  507.  
  508.            }
  509.           else
  510.           {
  511.             v->k[index1+1] = key;
  512.         u->same[1] = v;        
  513.         v->same[index1+1] = u ;  
  514.  
  515.             u->son[2]->son[1] = u;
  516.            }
  517.  
  518.          }
  519.  
  520.         if (v->p > b) split_node(v);
  521.  
  522.         return u;
  523.       }
  524.    else { clear_inf(w->inf);
  525.           w->inf = inf; 
  526.           return w;
  527.          }
  528.    };
  529.  
  530. ab_tree_node* ab_tree::locate(GenPtr key) const
  531.    {
  532.   /* searching for an element key in a (a,b)-tree ;
  533.    we search down the tree starting at the root r until we reache 
  534.    a leave. In each node v we use the sequence k[1](v),..k[v->p-1](v) 
  535.    in order to guide the search to the proper subtree.In the the
  536.    function we assume that k[0](v)<key<k[v->p](v) for every
  537.    element key in U
  538.    locate returns a pointer to a leave*/ 
  539.  
  540.   register ab_tree_node* v = root;
  541.   register int i;
  542.  
  543.   if (v == nil) return nil;
  544.  
  545.    while (v->p)      /* while v is not a leave*/
  546.    { for(i=1; i < v->p && cmp(key,v->k[i]) > 0; i++);   // s.n.
  547.      v=v->son[i];
  548.     };
  549.  
  550.   return (cmp(key,v->k[1]) <= 0) ? v : nil;
  551. };
  552.  
  553. ab_tree_node* ab_tree::locate_succ(GenPtr key) const
  554. { ab_tree_node* v = locate(key);
  555.   if (v==0) return maximum;
  556.   if (cmp(key,v->k[1])==0) return v;
  557.   return v->son[1];
  558.  }
  559.  
  560. ab_tree_node* ab_tree::locate_pred(GenPtr key) const
  561. { ab_tree_node* v = locate(key);
  562.   if (v==0) return maximum;
  563.   if (cmp(key,v->k[1])==0) return v;
  564.   return v->son[1];
  565.  }
  566.  
  567.  
  568. ab_tree_node* ab_tree::lookup(GenPtr k) const 
  569. { ab_tree_node* p = locate(k);
  570.   if (p && cmp(k,key(p))!=0 ) p = 0;
  571.   return p;
  572.  }
  573.  
  574.  
  575. void ab_tree::fuse(ab_tree_node* v,ab_tree_node* y)
  576.    {
  577.    /* fuse v and y, i.e. make all sons of y to sons of v and move all
  578.       keys from y to v and delete node y; also move one key (the key
  579.       between the pointers to y and v) from z to v; (note that this will
  580.       shrink z, i.e. decrease the arity of z by one)  
  581.    */
  582.  
  583.    ab_tree_node* z=v->father;
  584.  
  585.    GenPtr hilf=z->k[v->index] ;     /* before k[v->p] was not used {=0}*/
  586.               /* 
  587.                  we only must copy the pointer of son and the node-keys
  588.                  from y to v
  589.               */
  590.           /* change same-pointer of y and z                     */
  591.    v->k[v->p]=hilf; 
  592.    v->same[v->p]=z->same[v->index];  
  593.    v->same[v->p]->same[1] = v;       
  594.  
  595.    for(int i=1;i<y->p;i++) {  v->k[v->p+i]=y->k[i];
  596.                   v->same[v->p+i]=y->same[i];    
  597.                   v->same[v->p+i]->same[1]=v;    
  598.                               v->son[v->p+i]=y->son[i];
  599.                               v->son[v->p+i]->index=v->p+i;
  600.                               v->son[v->p+i]->father=v;
  601.                            };
  602.    v->son[v->p+y->p]=y->son[y->p];   /* copy of the last son from y*/
  603.    v->son[v->p+y->p]->index=v->p+y->p;
  604.    v->son[v->p+y->p]->father=v;
  605.  
  606.    v->p=v->p+y->p;
  607.  
  608.    for(i=v->index;i<z->p;i++) { z->k[i]=z->k[i+1];
  609.                 z->same[i] = z->same[i+1]; 
  610.                                 z->son[i+1]=z->son[i+2];
  611.                                 if (z->son[i+1]!=0){z->son[i+1]->index=i+1; 
  612.                                 z->son[i+1]->father=z;};
  613.                                };
  614.    z->p--;
  615.    z->k[z->p]=0;      
  616.    z->same[z->p]=0;   
  617.  
  618.    delete y;
  619.    
  620.   };
  621.  
  622.  
  623.  void ab_tree::share(ab_tree_node* v,ab_tree_node* y,int direct)
  624.  {
  625.   /* we assume that y is the right brother of v;
  626.      take the leftmost son away from y and make it an additional(right-
  627.      most) son of v; also move one key( the key between the pointers
  628.      to v and y) from z down to v and replace it by the leftmost
  629.      key of y;    
  630.      the other case is equivalent
  631.      let z be the father of v
  632.    */
  633.  
  634.      ab_tree_node* z=v->father;
  635.  
  636.      if (direct==1)  { 
  637.                        v->son[v->p+1]=y->son[1];
  638.                        v->son[v->p+1]->index=v->p+1;
  639.                        v->son[v->p+1]->father=v;
  640.                        v->k[v->p]=z->k[v->index];
  641.                v->same[v->p]=z->same[v->index];   
  642.                        v->same[v->p]->same[1] = v;    
  643.                        z->k[v->index]=y->k[1];
  644.                        z->same[v->index]=y->same[1];      
  645.                z->same[v->index]->same[1]=z;      
  646.  
  647.                        v->p++;    
  648.  
  649.                        for(int i=1;i<(y->p)-1;i++) 
  650.                        { y->son[i]=y->son[i+1];
  651.                          y->son[i]->index=i;
  652.                          y->k[i]=y->k[i+1];
  653.                          y->same[i]=y->same[i+1];
  654.                        };
  655.  
  656.                        y->p--;     // decrease number of children
  657.  
  658.                        y->son[y->p]=y->son[y->p+1];
  659.                        y->son[y->p]->index=y->p;
  660.                        y->son[y->p+1]=0;  
  661.                        y->k[y->p]=0;
  662.                y->same[y->p] = 0;                 
  663.                      }
  664.      else            {    // y is at the left side of v
  665.                        for(int i=v->p;i>=1;i--) 
  666.                        { v->son[i+1]=v->son[i];
  667.                          v->son[i+1]->index=i+1;
  668.                          v->k[i+1]=v->k[i];
  669.                          v->same[i+1]=v->same[i]; 
  670.                         };
  671.  
  672.                        v->son[1]=y->son[y->p];
  673.                        v->son[1]->index=1;
  674.                        v->son[1]->father=v;
  675.                        y->son[y->p]=0;
  676.  
  677.                        v->p++;
  678.                        y->p--;
  679.  
  680.                        v->k[1]=z->k[y->index];
  681.                v->same[1]=z->same[y->index];     
  682.                v->same[1]->same[1] = v;            
  683.                        z->k[y->index]=y->k[y->p];
  684.                        z->same[y->index]=y->same[y->p];  
  685.                        z->same[y->index]->same[1] = z;   
  686.                        y->k[y->p]=0;
  687.                y->same[y->p]=0;                    
  688.                      };
  689.      }
  690.  
  691.  
  692.  void ab_tree::del_item(ab_tree_node* w)
  693.     {
  694.          /* 
  695.           we must delete leave w with father v
  696.           we shrink v by deleting leave w and one of the keys in the 
  697.           adjacent to the pointer to w 
  698.           (if w is the i-th son of v then we delete k[i](v) if i<v->p
  699.           k[i-1](v) if i=v->p  ).
  700.       m.w. if i=v->p we overwrite the inner node 
  701.            in which key w->k[1] is stored with k[i-1](v)
  702.            and then delete k[i-1](v)
  703.          */
  704.  
  705.  
  706.        if (w==nil) error_handler(1,"ab_tree: nil item in del_item");
  707.  
  708.        count--;
  709.  
  710.        if (maximum==minimum)  
  711.         { maximum=minimum=root=0; 
  712.           height=-1; 
  713.           clear_key(w->k[1]);
  714.           clear_inf(w->inf);
  715.           delete w; 
  716.           return;
  717.          }
  718.             /* here w==root==> last leave will be deleted*/
  719.        
  720.        ab_tree_node* succ = w->son[2];
  721.        ab_tree_node* pred = w->son[1];
  722.  
  723.        if (pred) pred->son[2]   = succ;
  724.        else minimum = succ;
  725.        if (succ) succ->son[1] = pred;
  726.        else  maximum = pred;
  727.  
  728.  
  729.        ab_tree_node* v    = w->father;
  730.  
  731.        v->son[w->index]=0;
  732.        if (w->index==v->p) {
  733.                  if (succ)
  734.                  { //overwrite copies in inner node u
  735.                    ab_tree_node* u = w->same[1];
  736.                    int pos=1;
  737.                                while(u->same[pos]!=w) pos++;
  738.                    u->k[pos]=pred->k[1];  
  739.                    u->same[pos]=pred;
  740.                    pred->same[1] = u;
  741.                  }
  742.                  else
  743.                    v->same[v->p-1]->same[1]=0; 
  744.                              v->k[v->p-1]=0;
  745.                  v->same[v->p-1]=0;           
  746.                }
  747.        else                { v->k[w->index]=0 ;
  748.                  v->same[w->index]=0;         
  749.                              for(int i=w->index;i<(v->p)-1;i++)
  750.                              { v->k[i]=v->k[i+1];
  751.                                v->same[i]=v->same[i+1]; 
  752.                                v->son[i]=v->son[i+1];
  753.                                v->son[i]->father=v;
  754.                                v->son[i]->index=i;
  755.                               };
  756.                              v->son[v->p-1]=v->son[v->p];
  757.                              v->son[v->p]=0;
  758.                              v->k[v->p-1]=0;
  759.                              v->same[v->p-1]=0;          
  760.                              v->son[v->p-1]->father=v;
  761.                              v->son[v->p-1]->index=v->p-1;
  762.                            };
  763.  
  764.        clear_key(w->k[1]);
  765.        clear_inf(w->inf);
  766.        delete w;
  767.  
  768.        (v->p)--;
  769.        
  770.        if ((v==root) && (v->p==1)) {  
  771.                                   if (v->son[1]==0) 
  772.                                       {v->son[1]=v->son[2];
  773.                                        v->son[2]=0;  };
  774.                                   root=v->son[1];
  775.                                   delete v;
  776.                                   root->index=0;
  777.                                   root->father=0;
  778.                                   root->p=0;
  779.                                   root->height=0;
  780.                                   height=0;
  781.                                   minimum=maximum=root;
  782.                                   return; 
  783.                                 };
  784.  
  785.        if (v->p >= a) return;
  786.  
  787.     /* otherwise v needs to be rebalanced by either fusing or sharing
  788.      let y be any brother of v*/
  789.      ab_tree_node* z;
  790.      ab_tree_node* y;
  791.      int dir;
  792.        if (v->index==1)  {
  793.                             z=v->father;
  794.                             y=z->son[v->index+1] ;
  795.                            dir=1;    //  y is the right son of v
  796.                          }
  797.        else              { 
  798.                             z=v->father;
  799.                             y=z->son[v->index-1] ;
  800.                            dir =0;   //  y is the left son of v
  801.                          };
  802.        while ((v->p==a-1) && (y->p==a))
  803.             {
  804.              if (dir==1) fuse(v,y); 
  805.              else  fuse(y,v); 
  806.  
  807.              if (z==root) {
  808.                            if (z->p==1) {
  809.                                          if (dir==1){
  810.                                                       root=v;
  811.                                                       v->father=0;
  812.                                                       v->index=0;   }
  813.                                          else {
  814.                                                 root=y;
  815.                                                 y->father=0;
  816.                                                 y->index=0;    };
  817.                                          height--;
  818.                                          delete z;  
  819.                                        };
  820.                            return;
  821.                           };
  822.              v=z;       // continue recursion
  823.              if (v->index==1)  {z=v->father;
  824.                                 y=z->son[v->index+1] ;
  825.                                 dir=1;  // y is the right son of v
  826.                                }
  827.              else              { z=v->father;
  828.                                  y=z->son[v->index-1];
  829.                                  dir=0;
  830.                                };
  831.             };
  832.     /* we have either v->p>=a and rebalancing is completed or
  833.      v->p = a-1 and y->p > a and rebalancing is completed by sharing;*/
  834.  
  835.       if (v->p==a-1)
  836.         {       /*
  837.                  it is important to which side y is in order to v
  838.                  ==> dir is an information about in the function share
  839.                 */
  840.            share(v,y,dir);
  841.         };
  842.       return;
  843.  }
  844.  
  845.  
  846. ab_tree& ab_tree::conc(ab_tree& s2)
  847.  
  848.   if ((a!=s2.a)||(b!=s2.b)) 
  849.      error_handler(1,"ab_tree: incompatible trees in concatenate operation");
  850.  
  851.   if (s2.root==0) return *this;
  852.  
  853.   if (root==0) 
  854.   { root=s2.root;
  855.     maximum=s2.maximum;
  856.     minimum=s2.minimum;
  857.     height=s2.height;
  858.     count =s2.count;
  859.    }
  860.   else
  861.   { if (cmp(maximum->k[1],s2.minimum->k[1])>=0) 
  862.                     error_handler(1,"ab_tree: join(S,T) : max(S)>=min(T)"); 
  863.  
  864.     concat(*this,s2,maximum,maximum->k[1]);
  865.  
  866.     // link leaves 
  867.     maximum->son[2]=s2.minimum;       
  868.     s2.minimum->son[1]=maximum;
  869.  
  870.     maximum=s2.maximum;              
  871.    }
  872.  
  873.   s2.root=0;
  874.   s2.maximum=0;
  875.   s2.minimum=0;
  876.   s2.height=-1;
  877.  
  878.   return *this;
  879. }
  880.  
  881.  
  882. /*---------------------------------------------------------------------
  883.   global functions
  884. ----------------------------------------------------------------------*/
  885.  
  886. void concat(ab_tree& s1,ab_tree& s2,ab_tree_node* current,GenPtr cur_key)
  887.   // Result in s1
  888.  
  889.   ab_tree_node* v=s1.root;
  890.   ab_tree_node* w=s2.root;
  891.   int h1=v->height;     
  892.   int h2=w->height;
  893.   int i;
  894.  
  895.   if(h1==h2)
  896.      { ab_tree_node* z=new ab_tree_node(2,h1+1,0,0,s1.b);
  897.        z->son[1]=v;z->son[2]=w; z->k[1]=cur_key;
  898.        z->son[1]->father=z; z->son[2]->father=z;
  899.        z->son[1]->index=1;z->son[2]->index=2;
  900.        z->same[1]=current;              
  901.        current->same[1]=z;              
  902.        s1.height++;
  903.        s1.root=z;
  904.     }
  905.   else { if (h1>h2)
  906.          {
  907.             for(i=1;i<h1-h2;i++,v=v->son[v->p]);  
  908.             v->son[v->p+1]=w;
  909.             v->son[v->p+1]->father=v;
  910.             v->son[v->p+1]->index=v->p+1;
  911.             v->k[v->p]=cur_key;
  912.         v->same[v->p]=current;     
  913.         current->same[1]=v;        
  914.          v->p++;
  915.         if (v->p==s1.b+1)  {s1.split_node(v);  };
  916.         }
  917.         else /* h1<h2 */
  918.         {
  919.        for(i=1;i<=h2-h1-1;i++,w=w->son[1]);
  920.            for(i=w->p;i>1;i--)
  921.             { w->son[i+1]=w->son[i];
  922.               w->son[i+1]->father=w;
  923.             w->son[i+1]->index=i+1;
  924.               w->k[i]=w->k[i-1];
  925.           w->same[i]=w->same[i-1];   
  926.             };
  927.            w->p++;
  928.            w->son[2]=w->son[1];
  929.            w->son[2]->father=w;
  930.            w->son[2]->index=2;
  931.            w->son[1]=v;
  932.            w->son[1]->father=w;
  933.            w->son[1]->index=1;
  934.            w->k[1]=cur_key;
  935.        w->same[1]=current;             
  936.        current->same[1]=w;             
  937.            if (w->p==s2.b+1) {s2.split_node(w);};
  938.        s1.root =  s2.root;
  939.        s1.height =  s2.height;
  940.         }
  941.       }
  942.  
  943.   /* maximum/minimum are now undefined  */
  944.  
  945. }
  946.  
  947.  
  948.  
  949. /*
  950. void ab_tree::split_at_key(GenPtr y,ab_tree& L,ab_tree& R)
  951. { ab_tree_node* w = locate(y);
  952.   if (!w) error_handler(1,"ab_tree: split: no item ");
  953.   split_at_item(w,L,R);
  954.  }
  955. */
  956.  
  957. void ab_tree::split_at_item(ab_tree_node* w,ab_tree& L,ab_tree& R)
  958.   {
  959.     if(((a!=L.a)||(a!=R.a))||((b!=L.b)||(b!=R.b)))
  960.        error_handler(1,"ab_tree: incompatible trees in split operation");
  961.     
  962.     /* initialisation   */
  963.     L.root=L.minimum=L.maximum=0;L.height=-1;
  964.     R.root=R.minimum=R.maximum=0;R.height=-1;
  965.  
  966.     if(root==0) return;
  967.  
  968.     if (w==0) 
  969.     { R.root = root;
  970.       R.height = height;
  971.       R.maximum = maximum;
  972.       R.minimum = minimum;
  973.       R.count = count;
  974.       root = 0;
  975.       height = -1;
  976.       maximum = 0;
  977.       minimum = 0;
  978.       count = 0;
  979.       return;
  980.      }
  981.  
  982.     if (w==maximum) 
  983.     { L.root = root;
  984.       L.height = height;
  985.       L.maximum = maximum;
  986.       L.minimum = minimum;
  987.       L.count = count;
  988.       root = 0;
  989.       height = -1;
  990.       maximum = 0;
  991.       minimum = 0;
  992.       count = 0;
  993.       return;
  994.      }
  995.  
  996.     ab_tree_node* l;
  997.     ab_tree_node* r;    // pointers to the roots of the left and right subtree
  998.  
  999.  
  1000.     /* parameters for concat  */
  1001.  
  1002.     ab_tree_node* current_l=0 ;
  1003.     GenPtr           current_l_key=0;
  1004.  
  1005.     ab_tree_node* current_r=0;  
  1006.     GenPtr           current_r_key=0;
  1007.  
  1008.     int i;
  1009.  
  1010.  
  1011.     /* w is a pointer to the leave y  */
  1012.     ab_tree_node* v;
  1013.  
  1014.     /* store leaf to split at         */
  1015.     ab_tree_node* leaf=w;
  1016.  
  1017.  
  1018.      l = w;
  1019.      r = 0;
  1020.  
  1021.       do{
  1022.          v=w->father;
  1023.          int index_of_w = w->index; 
  1024.  
  1025.        /* now we have construct the  left and right subtrees and the pointers
  1026.           to the roots  --> we must construct two trees with these roots*/
  1027.  
  1028.         if ((L.root==0)&&(l!=0))  { L.root=l;
  1029.                         L.height=l->height; 
  1030.                         L.root->father=0;
  1031.                         L.root->index=0;
  1032.                       }
  1033.         else { if ((L.root!=0)&&(l!=0))
  1034.                  {  ab_tree L1(L.a,L.b);
  1035.                 L1.root=l;
  1036.                     L1.height=l->height;
  1037.                     L1.root->father=0;
  1038.                     L1.root->index=0;
  1039.                 concat(L1,L,current_l,current_l_key);
  1040.                 L.root  = L1.root;
  1041.                     L.height= L1.height;
  1042.                     L.count = L1.count;
  1043.  
  1044.                     L1.root=0;
  1045.              }
  1046.              }
  1047.        if ((R.root==0)&&(r!=0))  {R.root=r;
  1048.                       R.height=r->height;
  1049.                       R.root->father=0;
  1050.                       R.root->index=0;
  1051.                                   R.root->p=r->p;
  1052.                      }
  1053.        else { if ((R.root!=0)&&(r!=0))
  1054.                 { ab_tree R1(R.a,R.b);
  1055.             R1.root=r;
  1056.           R1.height=r->height;
  1057.                   R1.root->father=0;
  1058.                   R1.root->index=0;
  1059.                   R1.root->p=r->p;
  1060.           concat(R,R1,current_r,current_r_key);
  1061.                   R1.root=0;
  1062.             }
  1063.             }
  1064.         if (v!=0)
  1065.         {
  1066.          if (index_of_w==1)     /* w is leftmost son of v */
  1067.          { l=0;
  1068.            r=v;
  1069.            current_r=v->same[1];
  1070.            current_r_key=v->k[1];
  1071.        r->same[1]->same[1]=0;        
  1072.        for(i=2;i<r->p;i++) 
  1073.             {  r->son[i-1]=r->son[i];
  1074.               r->son[i-1]->index=i-1;
  1075.             r->k[i-1]=r->k[i];
  1076.            r->same[i-1]=r->same[i];  
  1077.             }
  1078.            r->son[r->p-1]=r->son[r->p];    /* last son */
  1079.            r->son[r->p-1]->index=r->p-1;
  1080.            r->son[r->p]=0;
  1081.            r->p--; 
  1082.            r->k[r->p]=0;
  1083.        r->same[r->p]=0;      
  1084.            if (r->p==1) r=r->son[1];
  1085.          } 
  1086.          else {if ( index_of_w==v->p )
  1087.                  {  r=0;
  1088.                     l=v;
  1089.             l->son[l->p]=0;  /* last son */
  1090.             l->p--;
  1091.             current_l=l->same[index_of_w-1];
  1092.             current_l_key=current_l->k[1];
  1093.                     l->k[l->p]=0;
  1094.             l->same[l->p]->same[1]=0;   
  1095.             l->same[l->p]=0;            
  1096.                     if (l->p==1) l=l->son[1];
  1097.         } 
  1098.                else  /* if w is not the leftmost or rightmost son of v*/
  1099.                {  
  1100.                  r=v;
  1101.                  l=new ab_tree_node(index_of_w-1,v->height,0,0,R.b);
  1102.           current_l=v->same[index_of_w-1];
  1103.           current_l_key=current_l->k[1];
  1104.          current_r=v->same[index_of_w];   
  1105.          current_r_key=current_r->k[1];
  1106.          // current_r=(v->k[index_of_w])-1;   ERROR: liefert neuen Schluessel ;
  1107.                  for(i=1;i<index_of_w-1;i++)
  1108.              {
  1109.            l->son[i]=v->son[i];
  1110.            l->son[i]->index=i;
  1111.                    l->son[i]->father=l;
  1112.            l->k[i]=v->k[i];
  1113.            l->same[i]=v->same[i];  
  1114.            l->same[i]->same[1]=l;  
  1115.           };
  1116.          l->son[index_of_w-1]=v->son[index_of_w-1];
  1117.          l->son[index_of_w-1]->index=index_of_w-1;
  1118.                  l->son[index_of_w-1]->father=l;
  1119.  
  1120.                  r->son[index_of_w] = 0;   // changed
  1121.  
  1122.              for (i=1;i<r->p-index_of_w;i++)
  1123.           {
  1124.            r->son[i]=r->son[i+index_of_w];
  1125.                    r->son[i+index_of_w]=0;
  1126.                     r->son[i]->index=i;
  1127.                    r->son[i]->father=r;
  1128.            r->k[i]=r->k[i+index_of_w];
  1129.            r->same[i]=r->same[i+index_of_w]; 
  1130.                    r->k[i+index_of_w]=0;
  1131.            r->same[i+index_of_w]=0;
  1132.           };
  1133.              r->son[r->p-index_of_w]=r->son[r->p];  /* last son */
  1134.                  r->son[r->p]=0;
  1135.          r->son[r->p-index_of_w]->index=i;
  1136.                  r->son[r->p-index_of_w]->father=r;
  1137.          r->p=r->p-index_of_w;
  1138.                  if (l->p==1) l=l->son[1];
  1139.                  if (r->p==1) r=r->son[1];
  1140.                 };
  1141.                };
  1142.               };
  1143.  
  1144.  /* initialisation for the next iteration  */
  1145.   w=v;
  1146.  }
  1147.  while (w!=0);
  1148.  
  1149.  
  1150.  /* unlink leaves    m.w.         */
  1151.  leaf->same[1]=0;
  1152.  leaf->son[2]->son[1]=0;
  1153.  leaf->son[2]=0;
  1154.  
  1155.  /* redefine maximum and minimum  */
  1156.  L.minimum=minimum;
  1157.  ab_tree_node* help=L.root;
  1158.  while (help->p) help=help->son[help->p];
  1159.  L.maximum=help;
  1160.  help=R.root;
  1161.  while (help->son[1]!=0) help=help->son[1];
  1162.  R.minimum=help;
  1163.  R.maximum=maximum;
  1164.  
  1165.  maximum=minimum=root=l=r=0;
  1166.  height=-1; 
  1167.  count = 0;
  1168.  
  1169.  delete l;
  1170.  delete r;
  1171.  
  1172. }
  1173.  
  1174. void ab_tree::pr_ab_tree(ab_tree_node* localroot,int blancs) const
  1175.  
  1176.   if (localroot==0)
  1177.    { for(int j=1;j<=blancs;j++) cout<<" ";
  1178.      cout << "NIL\n";
  1179.      return;
  1180.     }
  1181.   
  1182.   if (localroot->p == 0) 
  1183.    { for(int j=1;j<=blancs;j++) cout<<" ";
  1184.      print_key(localroot->k[1]); 
  1185. /*
  1186.      ab_tree_node* s= localroot->same[1];
  1187.      cout << " same = "; 
  1188.      if (s) print_key(s->k[1]); 
  1189.      else cout << "NIL";
  1190. */
  1191.      cout << "\n";
  1192.     }
  1193.  
  1194.    else
  1195.     { for(int i=1;i<localroot->p;i++)
  1196.       { pr_ab_tree(localroot->son[i],blancs+10);
  1197.         for(int j=1;j<=blancs;j++) cout<<" ";
  1198.         print_key(localroot->k[i]); 
  1199. /*
  1200.         cout << " same = "; 
  1201.         print_key(localroot->same[i]->k[1]); 
  1202. */
  1203.         cout << "\n";
  1204.        };
  1205.       pr_ab_tree(localroot->son[localroot->p],blancs+10);
  1206.     }
  1207.  
  1208. ab_tree_node* ab_tree::copy_ab_tree(ab_tree_node* localroot,
  1209.                                     abnode& last_leaf,int b) const
  1210.   ab_tree_node* r;
  1211.  
  1212.   if (localroot->p == 0)   //leaf
  1213.    { r=new ab_tree_node(localroot->p,localroot->height,localroot->index,0,1); 
  1214.  
  1215.      r->k[1]= localroot->k[1];
  1216.      r->inf = localroot->inf;
  1217.  
  1218.      copy_key(r->k[1]);
  1219.      copy_inf(localroot->inf);
  1220.  
  1221.      r->son[1]=last_leaf;
  1222.      if (last_leaf) last_leaf->son[2] = r;
  1223.      last_leaf = r;               
  1224.  
  1225.     }
  1226.   else
  1227.    { r=new ab_tree_node(localroot->p,localroot->height,localroot->index,0,b); 
  1228.  
  1229.      for(int i=1;i<localroot->p;i++)
  1230.      { r->son[i]=copy_ab_tree(localroot->son[i],last_leaf,b);
  1231.        r->son[i]->father=r;
  1232.        r->k[i]=localroot->k[i];
  1233.        last_leaf->same[1]=r;        
  1234.        r->same[i]=last_leaf;        
  1235.       }
  1236.  
  1237.      r->son[localroot->p]=copy_ab_tree(localroot->son[localroot->p],last_leaf,b);
  1238.      r->son[localroot->p]->father=r;
  1239.    }
  1240.  
  1241.   return r;
  1242. }
  1243.         
  1244. void ab_tree::del_tree(ab_tree_node* localroot)
  1245. { // deletes subtree  rooted at localroot
  1246.  
  1247.   int k = localroot->p;
  1248.  
  1249.   for(int i=1;i<=k;i++) del_tree(localroot->son[i]);
  1250.  
  1251.   if (k==0) //leaf
  1252.   { clear_key(localroot->k[1]);
  1253.     clear_inf(localroot->inf);
  1254.    }
  1255.  
  1256.   delete localroot;
  1257. }
  1258.  
  1259. void ab_tree::change_inf(ab_tree_node* p, GenPtr x) 
  1260. { clear_inf(p->inf);
  1261.   copy_inf(x);
  1262.   p->inf = x;
  1263.  }
  1264.  
  1265.